package com.hq.shop13sms.listener;

import com.hq.commons.constatns.RabbitMQConstatns;
import com.hq.commons.ov.Sms;
import com.hq.shop13sms.util.HttpUtils;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
public class SmsQueueListener {

    @Value("${sms.appCode}")
    private String appCode;

    @RabbitListener(queues = RabbitMQConstatns.SMS_QUEUE)
    public void addGoodsToEs(Sms sms, Channel channel, Message message) throws IOException {
        String host = "https://gyytz.market.alicloudapi.com";
//        接口路径
        String path = "/sms/smsSend";
//        请求方式
        String method = "POST";

//        把发送的数据封装到map中
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "APPCODE " + appCode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("mobile", sms.getPhone());
        querys.put("param", "**code**:" + sms.getCode() + ",**minute**:1");
        querys.put("smsSignId", sms.getSmsSignId());
        querys.put("templateId", sms.getTemplateId());
        Map<String, String> bodys = new HashMap<String, String>();

        try {
//            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
//            System.out.println(response.toString());
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}